using QFramework;
using QFramework.Game;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

namespace QGameFramework.Scenes
{
    public class SceneComponent : BaseComponent
    {
        public override void OnInit()
        {
            base.OnInit();
        }

        public override void DeInit()
        {
            base.DeInit();
        }

        /// <summary>
        /// 同步加载场景
        /// </summary>
        /// <param name="sceneName">场景名</param>
        /// <param name="callback">回调事件</param>
        public void LoadSceneSync(string sceneName, UnityAction<Scene, LoadSceneMode> callback = null)
        {
            var scene = SceneManager.GetSceneByName(sceneName);
            if (scene.isLoaded)
                return;
            QGFC.Instance.Resources.m_ResLoader.LoadSceneSync(sceneName, LoadSceneMode.Single);
            SceneManager.sceneLoaded += callback;
        }


        /// <summary>
        /// 同步加载场景
        /// </summary>
        /// <param name="sceneName">场景命</param>
        /// <param name="loadSceneMode">加载场景方式</param>
        /// <param name="callback">回调函数</param>
        public void LoadSceneSync(string sceneName, LoadSceneMode loadSceneMode,
            UnityAction<Scene, LoadSceneMode> callback = null)
        {
            var scene = SceneManager.GetSceneByName(sceneName);
            if (scene.isLoaded)
                return;
            QGFC.Instance.Resources.m_ResLoader.LoadSceneSync(sceneName, loadSceneMode);
            SceneManager.sceneLoaded += callback;
        }

        /// <summary>
        /// 加载场景
        /// </summary>
        /// <param name="sceneName">场景名</param>
        /// <param name="LoadSceneCallback">加载场景回调事件</param>
        /// <param name="PorgressCallback">加载场景进度回调事件</param>
        /// <param name="LoadSceneSuccess">加载啊场景成功回调事件</param>
        public void LoadSceneAsync(string sceneName, UnityAction<Scene, LoadSceneMode> LoadSceneCallback = null,
            UnityAction<float> PorgressCallback = null, UnityAction<bool> LoadSceneSuccess = null)
        {
            var scene = SceneManager.GetSceneByName(sceneName);
            if (scene.isLoaded)
                return;
            QGFC.Instance.Resources.m_ResLoader.LoadSceneAsync(sceneName, LoadSceneMode.Single,
                LocalPhysicsMode.None,
                (op) =>
                {
                    LogKit.I($"加载场景进度{op.progress}");
                    PorgressCallback?.Invoke(op.progress);
                    op.allowSceneActivation = true;
                    LoadSceneSuccess?.Invoke(op.isDone);
                    SceneManager.sceneLoaded += LoadSceneCallback;
                });
        }

        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="sceneName">场景名</param>
        /// <param name="loadSceneMode">加载场景模式</param>
        /// <param name="LoadSceneCallback">加载场景回调事件</param>
        /// <param name="PorgressCallback">加载场景进度回调事件</param>
        /// <param name="LoadSceneSuccess">加载场景成功回调事件</param>
        public void LoadSceneAsync(string sceneName, LoadSceneMode loadSceneMode,
            UnityAction<Scene, LoadSceneMode> LoadSceneCallback = null,
            UnityAction<float> PorgressCallback = null, UnityAction<bool> LoadSceneSuccess = null)
        {
            var scene = SceneManager.GetSceneByName(sceneName);
            if (scene.isLoaded)
                return;
            QGFC.Instance.Resources.m_ResLoader.LoadSceneAsync(sceneName, loadSceneMode,
                LocalPhysicsMode.None,
                (op) =>
                {
                    LogKit.I($"加载场景进度{op.progress}");
                    PorgressCallback?.Invoke(op.progress);
                    op.allowSceneActivation = true;
                    LoadSceneSuccess?.Invoke(op.isDone);
                    SceneManager.sceneLoaded += LoadSceneCallback;
                });
        }

        /// <summary>
        /// 卸载场景
        /// </summary>
        /// <param name="sceneName"></param>
        public void UnLoadSceneAsync(string sceneName)
        {
            SceneManager.UnloadSceneAsync(sceneName);
        }

        /// <summary>
        /// 卸载场景
        /// </summary>
        /// <param name="scene"></param>
        public void UnLoadSceneAsync(Scene scene)
        {
            SceneManager.UnloadSceneAsync(scene);
        }
    }
}